package fpzhan.plane.program.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import fpzhan.plane.program.connect.RunFlow;
import fpzhan.plane.program.manager.PlaneProgramManager;
import fpzhan.plane.program.param.Params;
import fpzhan.plane.program.test.school.*;
import fpzhan.plane.program.connect.RunResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProcessTest4 {

    public static RunFlow createFlow()throws Exception {
        Map map = new HashMap<>();
        map.put("class",new AClass("1","1"));

        RunFlow runFlow = PlaneProgramManager.createFlow("studentFlow");
        runFlow.initParam(map).add("class").comment("初始化参数为，一年级一班信息")
                .flowBegin()
                    .execute(Params.get(ClassInfos::new).add("classInfo")).comment("创建【班级基础信息】")
                    .execute(param -> param.put("schoolName","实验小学")).lose().add("schoolName").comment("设置【学校名称】")
                    .execute(Params.get(ArrayList::new).add("women")).comment("创建【班级女生集合】")
                    .execute(Params.get(ArrayList::new).add("men")).comment("创建【班级男生集合】")
                    .execute(Params.get("class",AClass.class).then(Query::teacherByClass).add("teachers")).comment("查询并设置【班级所有老师】")
                    .loop("teachers","teacher").comment("遍历获取每个老师，分管学生")
                        .execute(Params.get("teacher", Teacher.class).then(Query::studentByTeacher).add("teacherStudents")).comment("查询老师所有学生")
                        .loop("teacherStudents","student").comment("遍历老师的所有学生,并放入不同性别的的学生集合中")
                            .branch().comment("判断性别")
                                .ifs(Params.judege("student", Student.class).then(Student::isMan).compare(Object::equals,true)).comment("如果是男生")
                                    .execute(Params.get("men",List.class).updateLink(List::add,"student")).comment("将当前学生信息放入【班级男生集合】")
                                .exitIfs()
                                .defaults().comment("如果是女生")
                                    .execute(Params.get("women",List.class).updateLink(List::add,"student")).comment("将当前学生信息放入【班级女生集合】")
                                .exitDefaults()
                            .branchEnd()
                        .loopEnd()
                    .loopEnd()
                    .execute(
                            Params.get("classInfo",ClassInfos.class)
                                    .updateLink(ClassInfos::setTeachers,"teachers")
                                    .updateLink(ClassInfos::setMen,"men")
                                    .updateLink(ClassInfos::setWomen,"women")
                    ).comment("将【班级老师】、【班级男生】、【班级女生】放入【班级信息】中")
                .flowEnd();
        return runFlow;
    }

    public static void main(String[] args) throws Exception {
        //创建流程
        RunFlow runFlow=createFlow();
        //不用运行流程，就可以打印流程结构
        System.out.println(JSON.toJSONString(runFlow.struct(), SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
        //运行流程
        RunResult result = runFlow.run();
        //获取流程结果并打印
        Map<String,Object> data = result.getData();
        System.out.println(result.getData().toString());
        //获取流程代理结果
        JSONArray proxyResult = result.getProxyResult();
    }
}